Python is one of the most popular programming languages of 2021. Due to the vast community support and an insane number of libraries it is used in multiple domains. From web development to artificial intelligence, Python is everywhere. Like other programming languages, standard Python also has many operators.
What are the Operators? | Python Operators
Operators are the special symbols that operate on the operands and return a value as output. Mostly all the Python operators operate between two operands or values and return the output value in a specific Python Data Type . The return value and its data type depend upon the operands and operator. As we discussed, python has different operators; here is the list of different all the standard Python Operators:
- Arithmetic operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators mainly contain basic Math symbols. The arithmetic operator performs between two numeric variables or values and returns a value. Mostly the variables on which Arithmetic operators apply are Float and Integers.
Output
The return value's data type depends upon the type of Arithmetic Operator and data type of operands.
Arithmetic’s Operators | Operators’ Name | Example | Description |
+ | Addition | a + b | Addition operators Add Two Operands and return a value. E.g. print(2+3) Output: 5 |
- | Subtraction | a - b | The subtraction Operator returns the difference between the two operands. E.g. print(55-30) Output: 25 |
* | Multiplication | a * b | Multiply Operators return value by multiplying both the operands. E.g. print(5.5*6) Output: 33.0 |
/ | Division | a / b | Divide operators divide a by b and return a Float value e.g., print(55 / 5) Output: 11.0 |
% | Modulo | a % b | Modulus operators return the remainder of the value a when it is divided by b. e.g. print(5 % 4) Output: 1 |
** | Exponential | a ** b | Exponential operator raises the power of value a by b. e.g., print(3**2) Output: 9 |
// | Floor Division | a // b | Floor Division divides a by b and return the result but in the integer form. print(5// 2) Output: 2 |
2. Assignment Operators
Assignment operators assign the new value to the variable. The new value assigned to the variable could be any data type such as object, class, integer, float, list etc. When we call the assigned variable it returns the assigned value of the variable.
Output
The output of the Assignment operators depends upon the data type assigned to the Variable. When we assign any value to the variable it does not return anything instead it stores the assigned value.
Assignment Operators | Operators’ Name | Example | Discerption |
= | Assign | a = b | It will assign the value of b to a. e.g. a=5 print(a) Output: 5 |
+= | ADD and Assign | a += b | It can also be written as a=a+3. This will add 3 in a and assign new value to an a=3. a+=3 print(a) Output: 6 |
-= | Subtract and Assign | a -= b | It will subtract b from a, and assign the new value back to a. e.g. a=4 a-=2 print(a) Output: 2 |
*= | Multiply and Assign | a*=b | It will multiply b to a, and assign the new value to a. e.g. a=3 a*=4 print(a) Output: 12 |
/= | Divide and Assign | a/=b | It will divide a by b and assign the result to a. e.g. a = 3 a/=2 print(a) Output: 1.5 |
%= | Assign Reminder | a%=b | It will divide a by b and assign the reminder to a. e.g. a=5 a%=3 print(a) Output: 2 |
//= | Assign Floor Division | a//=b | It will divide a by b and assign the result to a but in integer form a=3 a//=2 print(a) Output: 1 |
**= | Assign Exponential | a**=b | It will raise the power of a by b and assign it back to a. a=2 a**=3 print(a) Output: 8 |
3. Comparison Operators
Comparison operators compare two values and return the output in the form of Boolean. If the comparison goes successful it returns ‘True’ otherwise ‘False’.
Output
The Comparison operators always return a Boolean value (True or False) depending upon the Operators and Operands.
Comparison Operators | Operators’ Name | Example | Discerption |
== | Equal to | a == b | It will compare that whether a and b are equal. e.g. 5==5 Output: True 5==6 Output: False |
!= | Not equal | a != b | If a and b are not equal it will return true else false. e.g. 5 != 5 Output: False 5 != 6 Output: True |
> | Greater Than | a > b | If a is greater than b it will return True else false. e.g. 5 > 4 Output: True 5 > 5 Output: False |
< | Less than | a < b | It will compare a with b, return True if a is smaller than b else False. e.g. 5 < 6 Output: True 6 < 5 Output: False |
>= | Greater than or equal to | a >= b | It will compare a with b and return True when a is greater or equal to b else it will return False. e.g. 4 >= 4 Output: True 7 >= 8 Output: False 74 >= 50 Output: True |
<= | Less than or equal to | a <= b | It will compare a with b and return True when a is less than or equal to b else it will return False. e.g. 4 <= 4 Output: True 7 <= 8 Output: True 74 <= 50 Output: False |
4. Logic Operators
Logic operators are used to comparing two statements and return a Boolean value, True or False, depending upon the statement. There are three types of logical operators
- and
- or
- not
Logical Operators | Operators’ Name | Example | Discerption |
And | and | x>y and y>z | It will return true if the statement on its left and right both are true else it will return false. e.g. 4 > 5 and 4 < 6 Output: False 4 > 3 and 2==2 Output: True |
Or | or | a == b or a>b | It will return True if either one of the statement is true if both the statement is false it returns False. e.g. 55 > 45 or 55 >65 Output: True 56 < 49 or 56 < 40 Output: False |
not | not | not(a > b) | It will return True if the state is False or it returns false if the statement is True. E.g. not(6 ==6) Output: False not( 7 < 4) True |
5. Identity Operators
Identity operators are special kinds of comparison operators that do not only compare the value but the data type too.
Output
The Identity operators return the Boolean value i.e. True or false.
Identity Operators | Operators’ Name | Example | Discerption |
is | Is Equal to | a is b it can be also written as a === b | It will compare that whether a and b are equal and have a same data type: e.g. 5 is 5 Output: True 5 is 5.0 Here 5 is an integer and 5.0 is a float. Output: False |
is not | Is Not Equal to | a is not b | If a and b are not equal it will return true else false: e.g. 5 is not 5 Output: False 5 is not 5.0 Output: True |
6. Membership Operators
The membership operator checks whether the given value is present or not. If the value which is asked is not there, it will return false else, it will return true: Return type: Boolean (True or False)
Membership Operators | Operators’ Name | Example | Discerption |
in | In | a in b | in operator checks whether a in b or not if a lay-in b it will return True else it will return False: e.g. 4 in [2,4,5,7] Output: True 4 in [2,3,4.0,45] Output: True 4 in [2,3,5] Output: False |
not in | Not in | a not in b | not in operator checks whether a in b or not if a lay-in b it will return False else it will return True: not in invert the value of in e.g. 4 in [2,4,5,7] Output: False 4 in [2,3,4.0,45] Output: False 4 in [2,3,5] Output: True |
7. Bitwise Operator
In python 0 is treated as False, and other than 0 and ‘None’ treated as True. So here are some bitwise operators which are not often used in Python.
Bitwise Operator | Name |
& | AND |
| | OR |
^ | XOR |
~ | NOT |
<< | Zero fill left shift |
>> | Signed right shift |